package org.jeecg.modules.iot.util;

import com.aliyun.mns.client.CloudAccount;
import com.aliyun.mns.client.CloudQueue;
import com.aliyun.mns.client.MNSClient;
import com.aliyun.mns.model.Message;
import org.jeecg.modules.iot.aliyun.MessageBody;
import org.jeecg.modules.iot.tools.JsonUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * 阿里云物联网client
 *
 * @author scw
 * @version v1.0
 * @date
 */
@Configuration
public class MnsUtil {

    @Value("${iot.accessKeyId}")
    private String accessKeyId;
    @Value("${iot.accessKeySecret}")
    private String accessKeySecret;
    @Value("${iot.regionId}")
    private String regionId;
    @Value("${iot.productKey}")
    private String productKey;
    @Value("${iot.iotInstanceId}")
    private String iotInstanceId;
    @Value("${iot.mnsEndpoint}")
    private String mnsEndpoint;

    private CloudAccount cloudAccount;


    /**
     * MNS消息服务
     * @return
     */
    @Bean
    public void initCloudAccount()  {
        String endPoint = String.format("http://%s",mnsEndpoint);
        this.cloudAccount = new CloudAccount(accessKeyId, accessKeySecret, endPoint);
    }

    /**
     * 接收消息
     * @param queueName
     */
    public void receiveMnsMessage(String queueName){
        MNSClient client = this.cloudAccount.getMNSClient();
        CloudQueue queue = client.getQueueRef(queueName);

        // 一次性拉取最多xx条消息
        int batchSize = 15;
        // 长轮询时间为 xx s
        int waitSeconds = 15;

        List<Message> messages = queue.batchPopMessage(batchSize, waitSeconds);
        if (messages != null && messages.size() > 0) {
            for (Message message : messages) {
                /**
                 * 1、处理消息
                 */
                MessageBody messageBody = JsonUtils.toObject(message.getMessageBody(), MessageBody.class);


                /**
                 * 2、删除已读消息
                 */
                queue.deleteMessage(message.getReceiptHandle());
            }
        }

        client.close();
    }

    /**
     * 发送消息
     * @param queueName
     * @param msg
     */
    public void sendMnsMessage(String queueName,String msg){
        MNSClient client = this.cloudAccount.getMNSClient();
        CloudQueue queue = client.getQueueRef(queueName);
        Message message = new Message();
        message.setMessageBody(msg);

        Message putMsg = queue.putMessage(message);

        client.close();
    }

    public CloudAccount getCloudAccount() {
        return cloudAccount;
    }

    public String getAccessKeyId() {
        return accessKeyId;
    }

    public String getAccessKeySecret() {
        return accessKeySecret;
    }

    public String getRegionId() {
        return regionId;
    }

    public String getProductKey() {
        return productKey;
    }

    public String getIotInstanceId() {
        return iotInstanceId;
    }

    public String getMnsEndpoint() {
        return mnsEndpoint;
    }
}
